﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Player : MonoBehaviour {

    private Vector3 boom_BagPos_World;
    private Vector3 potion_BagPos_World;
    private Vector3 god_BagPos_World;
    private Vector3 timeback_BagPos_World;
    public AudioClip win;
    public AudioClip die;
    public AudioClip collect;
    public GameObject EffectStar;
    public GameObject effectStar;
    public GameObject effectBoom;
    public GameObject effectPotion;
    public GameObject effectGod;
    public GameObject effectTimeback;
    public GameObject effectwin1;
    public GameObject effectwin2;
    public GameObject effectwin3;
    public GameObject usePotion;
    public TimeManager timeManager;
    public int lifeCount=3;
    public bool IsPlayerAlive = true;
    [SerializeField]
    private Transform startPos;

    private GameObject pos;
    private static Player _instance;
    public static Player Instance
    {
        get
        {
            return _instance;
        }
    }

    private void Start()
    {
        _instance = this;
        Initalize();
        boom_BagPos_World = Camera.main.ScreenToWorldPoint(new Vector3(UIManager.Instance.boom_BagPos_Screen.position.x, UIManager.Instance.boom_BagPos_Screen.position.y, UIManager.Instance.boom_BagPos_Screen.position.z));
        potion_BagPos_World = Camera.main.ScreenToWorldPoint(new Vector3(UIManager.Instance.potion_BagPos_Screen.position.x, UIManager.Instance.potion_BagPos_Screen.position.y, UIManager.Instance.potion_BagPos_Screen.position.z));
        god_BagPos_World = Camera.main.ScreenToWorldPoint(new Vector3(UIManager.Instance.god_BagPos_Screen.position.x, UIManager.Instance.god_BagPos_Screen.position.y, UIManager.Instance.god_BagPos_Screen.position.z));
        timeback_BagPos_World = Camera.main.ScreenToWorldPoint(new Vector3(UIManager.Instance.timeback_BagPos_Screen.position.x, UIManager.Instance.timeback_BagPos_Screen.position.y, UIManager.Instance.timeback_BagPos_Screen.position.z));
    }
    public void Initalize()
    {
        //player在初始位置
        lifeCount = 3;
        IsPlayerAlive = true;
        transform.DOMove(new Vector3( startPos.position.x,startPos.position.y,startPos.position.z), 0.1f,false);
        transform.DORotate(new Vector3(startPos.rotation.x, startPos.rotation.y, startPos.rotation.z), 0.1f);
        effectwin1.SetActive(false);
        effectwin2.SetActive(false);
        effectwin3.SetActive(false);
        Debug.Log("back to start");
    }

    private void FixedUpdate()
    {
        //如果按下1，使用道具炸弹
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            
            if(UIManager.Instance.GetPropsCount("Boom")>0)
            {
                //道具数量减一
                UIManager.Instance.ChangeProps("Boom", -1);
                //特效
                //效果
            }
        } 

        //如果按下2，使用道具药水
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            
            if (UIManager.Instance.GetPropsCount("Potion") > 0)
            {
                //道具数量减一
                UIManager.Instance.ChangeProps("Potion", -1);
                //取消Collider
                RaycastHit hit;
                Physics.Raycast(transform.position, transform.forward, out hit, 10);
                if (hit.transform != null)
                {
                    pos = hit.collider.gameObject;
                    hit.collider.GetComponent<MeshCollider>().enabled = false;
                    Invoke("AddMesh", 2);
                }

                usePotion.SetActive(true);
                Destroy(usePotion, 2f);
            }
        }
       
        //如果按下1，使用道具无敌
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            if (UIManager.Instance.GetPropsCount("Unbeatable") > 0)
            {
                //道具数量减一
                UIManager.Instance.ChangeProps("Unbeatable", -1);
                //特效
                //效果
            }
        }

        //如果按下1，使用道具时间倒流
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            if (UIManager.Instance.GetPropsCount("TimeBack") > 0)
            {
                //道具数量减一
                UIManager.Instance.ChangeProps("TimeBack", -1);
                //特效
                //效果
                timeManager.ChangeTime(5);
            }
        }

    }
    private void AddMesh()
    {
        pos.AddComponent<MeshCollider>();
    }
    public void OnTriggerEnter(Collider other)
    {
        //如果是道具
        Debug.Log("collide");
        LayerMask PropMask = 1 << LayerMask.NameToLayer("Props");
        LayerMask BarrierMask  = 1 << LayerMask.NameToLayer("Barrier");
        if (other.gameObject.layer==9)
        {
            Debug.Log("collide prop");
           
            //道具的数量加一
            UIManager.Instance.ChangeProps(other.tag,1);
            AudiosManager.Instance.AudioPlay(collect);
            //并显示特效
            switch (other.tag)
            {
                case "Boom":
                    EffectStar.gameObject.SetActive(true);
                    Invoke("CloseEffectStart", 1f);
                    //GameObject effect1=GameObject.Instantiate(effectStar);
                    //effect1.transform.position = other.transform.position;
                    //effect1.transform.DOMove(boom_BagPos_World, 1f).SetEase(Ease.InBounce);
                    //Destroy(effect1, 2f);
                    break;
                case "Potion":
                    EffectStar.gameObject.SetActive(true);
                    Invoke("CloseEffectStart", 1f);
                    //GameObject effect2 = GameObject.Instantiate(effectStar);
                    //effect2.transform.position = other.transform.position;
                    //effect2.transform.DOMove(boom_BagPos_World, 1f).SetEase(Ease.InBounce);
                    //Destroy(effect2, 2f);
                    //// GameObject.Destroy(effect, 5f);

                    break;
                case "Unbeatable":
                    EffectStar.gameObject.SetActive(true);
                    Invoke("CloseEffectStart", 1f);
                    //GameObject effect3 = GameObject.Instantiate(effectStar);
                    //effect3.transform.position = other.transform.position;
                    //effect3.transform.DOMove(boom_BagPos_World, 1f).SetEase(Ease.InBounce);
                    //Destroy(effect3, 2f);
                    break;
                case "TimeBack":
                    EffectStar.gameObject.SetActive(true);
                    Invoke("CloseEffectStart", 1f);
                    //GameObject effect4 = GameObject.Instantiate(effectStar);
                    //effect4.transform.position = other.transform.position;
                    //effect4.transform.DOMove(boom_BagPos_World, 1f).SetEase(Ease.InBounce);
                    //Destroy(effect4, 2f);
                    break;
                default:
                    break;
            }
            // Destroy(other.gameObject);
            other.gameObject.SetActive(false);
        }
        else if(other.gameObject.layer == 10)
        {
            Debug.Log("collide barrier");
            if(lifeCount<=0)
            {
                //游戏结束,角色不再受玩家的控制
                AudiosManager.Instance.AudioPlay(die);
                UIManager.Instance.ShowFailedPanel();
                IsPlayerAlive = false;

            }
            else
            {
                lifeCount--;
                //特效
                UIManager.Instance.ChangeLifeSprite(lifeCount);
            }
        }
       
    }

    private void CloseEffectStart()
    {
        EffectStar.gameObject.SetActive(false);
    }
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag=="Win")
        {
            AudiosManager.Instance.AudioPlay(win);
            collision.gameObject.transform.DOMoveY(1.2f, 2);
            effectwin1.SetActive(true);
            effectwin2.SetActive(true);
            effectwin3.SetActive(true);
            Invoke( "ShowWinPanel",10);
           
        }
    }
    private void ShowWinPanel()
    {
        UIManager.Instance.ShowWinPanel();
    }
   
}
